var {
  http
} = require('../../utils/apiutils');
const qiniu = require("../../utils/qnutils");

var config = require("../../utils/config")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    min: 0,
    max: 100,
    content: "",
    telphone: "",
    imgurl:config.imageUrl,
    fileList: [],
    uplist: [],
    upImg: [],
    dataList: [],
    pimg:[],
    free:false

  },
  deleteImg: function (e) {
    let index = e.detail.index;
    this.data.fileList.splice(index, 1);
    this.setData({
      fileList: this.data.fileList,
    })

  },
  deleUp:function(e)
  {
    var item = e.currentTarget.dataset.item;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定要删除改照片吗',
      success:function(res)
      {
        if(res.confirm)
        {
          that.deletePhoto(item.id)
        }
          
      }

    })

  },
  deletePhoto:function(id){

    var that = this;
    var data = {
            id:id
    }
    http.deletePhoto(data,
      function (res) {
        if(res.status == 200)
        {
          wx.showToast({
            title: '删除成功',
            icon:"none"
          })
          that.getphoheList()
        }
        else
        {
          wx.showToast({
            title: res.message,
            icon:"none"
          })
        }
       
      },
      function (err) {

      })

  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    console.log("---------", file.path);
    var data = {
      url: file.url
    }
    this.data.fileList.push(data);
    this.data.uplist.push(data);
    this.setData({
      fileList: this.data.fileList,
    })


    // wx.uploadFile({
    //   url: 'https://example.weixin.qq.com/upload', // 仅为示例，非真实的接口地址
    //   filePath: file.path,
    //   name: 'file',
    //   formData: { user: 'test' },
    //   success(res) {
    //     // 上传完成需要更新 fileList
    //     const { fileList = [] } = this.data;
    //     fileList.push({ ...file, url: res.data });
    //     this.setData({ fileList });
    //   },
    // });
  },
  onclickR:function(e){
    var item = e.currentTarget.dataset.item;
    wx.previewImage({
      current: this.data.imgurl+item.url, // 当前显示图片的http链接
      urls:  this.data.pimg // 需要预览的图片http链接列表
    })
  },
  upDataImage: function (tempFilePaths, index) {
    var that = this;
    // console.log("0000000000",tempFilePaths,"------------",index);
    qiniu.getUploadImgae(tempFilePaths,
      function (res) {
        console.log("--------上传后返回的数据--", index, res);
        debugger
        that.data.upImg.push(res.key);
        if (that.data.fileList.length - 1 == index) {
          that.uploading()
        } else {
          that.upDataImage(that.data.fileList[index + 1].url, index + 1);
        }
      },
      function (err) {})
  },
  getUpToken: function () {
    var that = this;
    http.getUpToken({},
      function (res) {
        console.log("----七牛云的授权码---", res);
        that.data.token = res.data;
        qiniu.initQiniu(res.data);
        that.data.upImg = []
        that.upDataImage(that.data.fileList[0].url, 0);
      },
      function (err) {
      })
  },

  uploading: function () {
    var data = {
      "urls": this.data.upImg,
    }
    var that = this;
    http.temporaryUploading(data,
      function (res) {
        if (res.status == 200) {
          wx.showToast({
            title: '上传成功',
            icon: "none",
          })
          that.setData({
            fileList:[],
          })
          that.getphoheList()
        } else {

          wx.showToast({
            title: res.message,
            icon: "none",
          })

        }
      },
      function (err) {

      })


    console.log("--------反馈建议提交的信息----", data);

  },
  onClickSumbit: function () {

    if (this.data.fileList.length > 0) {
      this.getUpToken()
    } else {
      wx.showToast({
        title: '请选择证件图片后进行上传',
        icon: "none",
      })
    }
  },

  beforeRead(event) {
    const {
      file,
      callback
    } = event.detail;
    callback(file.type === 'image');
  },
  inputs: function (e) {
    var value = e.detail.value;
    this.data.content = value;
    var len = parseInt(value.length);
    if (len > this.data.max) return;
    this.setData({
      currentWordNumber: len
    });
    if (this.data.currentWordNumber == 100) {
      wx.showModal({
        title: '提示',
        content: '您输入的文字已达上限',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.getphoheList()
    this.setData({
      free:new Date().getTime()>new Date(getApp().globalData.showtime.replace(/-/g, '/')).getTime()
    })
  },
  getphoheList: function () {

    var that = this;
    http.getPhoto({},
      function (res) {
        if (res.status == 200) {
          that.setData({
            dataList: res.data,
          })
         var arr = []
        res.data.forEach(element => {
          arr.push(that.data.imgurl + element.url);
        });
        that.data.pimg = arr;

        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })

        }
        // console.log("------------", res)
      },
      function (err) {

      })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})